/**
 * related to earlyPay.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var notify = sm("do_Notification");
var Controller = require("core/controller");
var device=require("util/device")
var log = require("util/log")
var navigate = require("util/navigate")
var router = require("config/router")
var server = require('config/server');
var net = require("util/net");
var page = sm("do_Page");
var dialog=sm("do_Dialog");
Controller({
	config : {
		header : {
			title : '提前结清'
		}
	},
	mapping : {
		"earlyPay_sum.text":"earInfo.earlyPay_sum",
		"earlyPay_Money.text":"earInfo.earlyPay_Money",
		"ea_reduceMoney.text":"earInfo.ea_reduceMoney",
		"ea_reduMark.text":"earInfo.ea_reduMark",
		"earlyPay_Style.items" : "earInfo.earlyPay_Style|getTypeCodeData",
		"earlyPay_Style.value" : "earInfo.earlyPay_StyleValue",
		"cas_earTop.items" : "earInfo.cas_earTop|getTypeCodeData",
		"cas_earTop.value" : "earInfo.cas_earTopValue",
		
//		"ear_leftImg.source":"earInfo.ear_leftImg",
//		"ear_rightImg.source":"earInfo.ear_rightImg"
	},
	data : {
		earInfo : {
			earlyPay_Money:"",
			ea_reduceMoney:"",
			ea_reduMark:"",
			earlyPay_Style: "0027",
			earlyPay_StyleValue:"103",
			cupoId:page.getData().cupoId,
			earlyPay_sum:"",
			cas_earTop: "0039",
			cas_earTopValue:"133",
//			ear_leftImg:"",
//			ear_rightImg:""
		},
		dialogData:{
//			capital:"",
//			interest:"",
//			overDueDisincentive:"",
//			contractAmount:"",
//			prepaymentAmt:"",
//			stageServiceAmt:"",
//			dueFine:"",
//			backServiceAmt:"",
//			otherAmt:""
		},
		earList:[],
		earImg:[],
		earpayMoney:{}
	},
	ui: {
		ea_btnConfirm:"ea_btnConfirm",
		ea_reduceMoney:"ea_reduceMoney",
		layout_ear:"layout_ear",
		ea_reduMark:"ea_reduMark",
		earlyPay_Money:"earlyPay_Money",
		root:"$"
	},
	event : {
		'ea_btnConfirm':{	
			'touch':function(){
				if(this.data.earInfo.earlyPay_Money.length>0){
					if((this.data.earInfo.ea_reduceMoney-288.88)<=0){
						this.submitEarlyPay()
					}else {
						notify.toast('减免金额应为0-288.88')
					}			
				}else{
					notify.toast('还款金额不能为空')
				}
			}
		},
		"dialog":{
			'touch':function(){
				dialog.open('source://view/dialog/earlypay_dialog.ui',this.data.dialogData)	
			}
		},  
		"early_upload":{
			'touch':function(){
			    device.uploadImage({
			    width: 480,
				height: 720,
			    iscut:false,
				quality:100,
				scope:this
			    }, this.ear_upLoad)
			}
		},
		'cas_earTop':{
			'selectChanged':function(){
				if(this.data.earInfo.cas_earTopValue==133){
					this.ui.layout_ear.visible=false
					this.ui.ea_reduMark.visible=false
					this.ui.earlyPay_Money.text=""
					this.ui.earlyPay_Money.enabled=true
					this.ui.root.redraw()
				}else if (this.data.earInfo.cas_earTopValue==134) {
					this.ui.ea_reduMark.visible=false
					this.ui.layout_ear.visible=false
					this.ui.earlyPay_Money.text=this.data.earData.ear.cupoEarlymoney
					this.data.earpayMoney=parseInt(this.data.earData.ear.cupoEarlymoney)
					this.ui.earlyPay_Money.enabled=false
					this.ui.earlyPay_Money.fontColor="#000000FF"
					this.ui.ea_btnConfirm.bgColor="#28313eFF"
					this.ui.root.redraw()
				}else {
					this.ui.ea_reduMark.visible=true
					this.ui.layout_ear.visible=true
					this.ui.earlyPay_Money.enabled=false
					this.ui.ea_btnConfirm.bgColor="#A9A9A9FF"
					this.ui.earlyPay_Money.fontColor="#000000FF"
					this.ui.root.redraw()
				}
				log.info(988,this.data.earInfo.cas_earTopValue)
			}
		},
		'ea_reduceMoney':{
			'textChanged':function(){
				this.ui.earlyPay_Money.text=((parseInt(this.data.earData.ear.cupoEarlymoney)-this.ui.ea_reduceMoney.text))
				parseInt(this.data.earData.ear.cupoEarlymoney)
				this.data.earpayMoney=((parseInt(this.data.earData.ear.cupoEarlymoney)-this.data.earInfo.ea_reduceMoney))
				if ((this.data.earInfo.ea_reduceMoney-288.88)>0) {
					notify.toast('减免金额应为0-288.88')
				}
				this.ui.ea_btnConfirm.bgColor="#28313eFF"
			}
		},
		'earlyPay_Money':{
			'textChanged':function(){
				this.ui.ea_btnConfirm.bgColor="#28313eFF"
			}
		}
	},
	methods : {
		ear_upLoad:function(path){
			this.data.earList.push(path.id)			
		},
		myEarly:function(myear){
			this.data.earInfo.earlyPay_sum=myear.ear.cupoEarlymoney
			this.refresh('$')
		},
		submitEarlyPay : function() {
			net.send({
				server:server.organization.overDayRepyMoney,
				data:{
					"cupoId": this.data.earData.ear.cupoId,
					  "derateDescripton": this.data.earInfo.ea_reduMark,
					  "derateFee": this.data.earInfo.ea_reduceMoney,
					  "fileIds": this.data.earList,
					  "payAmt": this.data.earInfo.earlyPay_Money,
					  "payaType": this.data.earInfo.cas_earTopValue,
					  "paymentType": this.data.earInfo.earlyPay_StyleValue
				},
				success:function(){	
					notify.toast("提交成功")
					navigate.back("6666")
				},
				error : function(data) {
					notify.toast(data.reason)
				},
				scope:this
			})
		}
	},
	loaded : function(data) {
		this.data.earData=data
		this.myEarly(this.data.earData)
		var get_data=page.getData().cupoOrdernum;
		log.info(get_data);
		net.send({
			server:server.organization.openDialog,
			data:{
				'orderNum':get_data
			},
			success:function(data){
				log.info("dialog data success")
				log.info(data)
				for(i in data){
					if(data[i]==null){
						data[i]=''
					}
				}
				this.data.dialogData=data
				log.info(this.data.dialogData)
				
			},
			error:function(data){
			notify.toast(data.reason)
			},
			scope:this
		})
	},
})